<!DOCTYPE html>
<meta charset='utf-8'>
<title>registerContentHandler()</title>

<script src='/resources/testharness.js'></script>
<script src='/resources/testharnessreport.js'></script>

<noscript><p>Enable JavaScript and reload.</p></noscript>

<p><strong>Note:</strong> If your browser limits the number of handler
registration requests on a page, you might need to disable or significantly
increase that limit for the tests below to run.</p>


<div id='log'></div>

<script>
test(function () {
  assert_idl_attribute(navigator, 'registerContentHandler');
}, 'the registerContentHandler method should exist on the navigator object');

/* Happy path */
test(function () {
  navigator.registerContentHandler('text/x-unknown-type', location.href + '/%s', 'foo');
}, 'a handler with valid arguments should work');


/* URL argument */
test(function () {
  navigator.registerContentHandler('text/x-unknown-type', '%s', 'foo');
}, 'a relative URL should work');

test(function () {
  navigator.registerContentHandler('text/x-unknown-type', location.href + '#%s', 'foo');
}, 'a URL with a fragment identifier should work');

test(function () {
  navigator.registerContentHandler('text/x-unknown-type', location.href + '?foo=%s', 'foo');
}, 'a URL with a query string should work');

test(function () {
  navigator.registerContentHandler('text/x-unknown-type', location.href + '?foo=%s&bar', 'foo');
}, 'a URL with a multi-argument query string should work');

test(function () {
  navigator.registerContentHandler('text/x-unknown-type', location.href + '/%s/bar/baz/', 'foo');
}, 'a URL with the passed string as a directory name should work');

test(function () {
  navigator.registerContentHandler('text/x-unknown-type', location.href + '/%s/bar/baz/?foo=1337&bar#baz', 'foo');
}, 'a URL with the passed string as a directory name followed by a query string and fragment identifier should work');

test(function () {
  navigator.registerContentHandler('text/x-unknown-type', location.href + '/%s/foo/%s/', 'foo');
}, 'a URL with the passed string included twice should work');

test(function () {
  assert_throws('SYNTAX_ERR', function () { navigator.registerContentHandler('text/x-unknown-type', '', 'foo') } );
}, 'an empty url argument should throw SYNTAX_ERR');

test(function () {
  assert_throws('SYNTAX_ERR', function () { navigator.registerContentHandler('text/x-unknown-type', 'http://%s.com', 'foo') } );
}, '%s instead of domain name should throw SYNTAX_ERR');

test(function () {
  assert_throws('SYNTAX_ERR', function () { navigator.registerContentHandler('text/x-unknown-type', 'http://%s.example.com', 'foo') } );
}, '%s instead of subdomain name should throw syntax_err');

test(function () {
  assert_throws('SYNTAX_ERR', function () { navigator.registerContentHandler('text/x-unknown-type', location.href + '', 'foo') } );
}, 'a url argument without %s should throw SYNTAX_ERR');

test(function () {
  assert_throws('SYNTAX_ERR', function () { navigator.registerContentHandler('text/x-unknown-type', 'http://example.com', 'foo') } );
}, 'a url argument pointing to a different domain name, without %s should throw SYNTAX_ERR');

test(function () {
  assert_throws('SYNTAX_ERR', function () { navigator.registerContentHandler('text/x-unknown-type', location.href + '/%', 'foo') } );
}, 'a url argument without %s (but with %) should throw SYNTAX_ERR');

test(function () {
  assert_throws('SYNTAX_ERR', function () { navigator.registerContentHandler('text/x-unknown-type', location.href + '/%a', 'foo') } );
}, 'a url argument without %s (but with %a) should throw SYNTAX_ERR');

test(function () {
  assert_throws('SECURITY_ERR', function () { navigator.registerContentHandler('text/x-unknown-type', 'http://example.com/%s', 'foo') } );
}, 'a url argument pointing to a different domain name should throw SECURITY_ERR');

test(function () {
  assert_throws('SECURITY_ERR', function () { navigator.registerContentHandler('text/x-unknown-type', 'https://example.com/%s', 'foo') } );
}, 'a url argument pointing to a different domain name should throw SECURITY_ERR (2)');

test(function () {
  assert_throws('SECURITY_ERR', function () { navigator.registerContentHandler('text/x-unknown-type', 'http://foobar.example.com/%s', 'foo') } );
}, 'a url argument pointing to a different domain name should throw SECURITY_ERR (3)');

/* Content type argument */

/* The following MIME types are handled natively by the browser, and must not
 * be possible to override. Note that this list only covers a few basic content
 * types. Full lists of content types handled by each browser is found under
 * /vendor/. */

var blacklist = new Array(
  'image/jpeg',
  'text/html',
  'text/javascript',
  'text/plain');

for (var bi=0, bl=blacklist.length; bi<bl; ++bi){

  test(function () {
    assert_throws('SECURITY_ERR', function () { navigator.registerContentHandler(blacklist[bi], location.href + '/%s', 'foo') } );
  }, 'attempting to override the ' + blacklist[bi] + ' MIME type should throw SECURITY_ERR');

}

/* Overriding the following MIME types should be possible. */
var whitelist = new Array('application/atom+xml', /* For feeds. */
  'application/rss+xml', /* For feeds. */
  'application/x-unrecognized', /* Arbitrary MIME types should be overridable. */
  'text/unrecognized',
  'foo/bar');

for (var wi=0, wl=whitelist.length; wi<wl; ++wi){

  test(function () {
    navigator.registerContentHandler(whitelist[wi], location.href + '/%s', 'foo');
  }, 'overriding the ' + whitelist[wi] + ' MIME type should work');

}

</script>

</body>
</html>
